// -----------------------------------------------------------------------
//  <copyright file="RoleResourceConfiguration.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2025 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2025/1/7 0:05</last-date>
// -----------------------------------------------------------------------

using DaprPlus.Authorization.Domain.Entities;
using DaprPlus.Identity.Domain.Entities;


namespace DaprPlus.Identity.Infrastructure.EntityFrameworkCore.Configurations;

public class RoleResourceConfiguration : EntityConfigurationBase<RoleResource>
{
    /// <summary>
    /// 重写以实现实体类型各个属性的数据库配置
    /// </summary>
    /// <param name="builder">实体类型创建器</param>
    public override void Configure(EntityTypeBuilder<RoleResource> builder)
    {
        builder.Property(m => m.OperationsJson).HasMaxLength(1000);
        builder.Property(m => m.FilterGroupsJson).HasMaxLength(-1);

        builder.HasIndex(m => new { m.RoleId, m.ResourceId }).HasDatabaseName("Role_Resource_Index").IsUnique();

        builder.HasOne(m => m.Resource).WithMany(n => n.RoleResources).HasForeignKey(m => m.ResourceId).IsRequired()
            .OnDelete(DeleteBehavior.Cascade);
        builder.HasOne<Role>().WithMany().HasForeignKey(m => m.RoleId).IsRequired()
            .OnDelete(DeleteBehavior.Cascade);
    }
}